#!/usr/bin/env python3
import sys
import pygame
from pygame import mixer

# 初始化Pygame和混音器
pygame.init()
mixer.init()

# 加载声音样本
tone440 = mixer.Sound("tone440.wav")
tone880 = mixer.Sound("tone880.wav")

# 计算节拍间隔时间，假设每分钟bpm拍
def calculate_beat_interval(bpm):
    return 60000 / bpm

# 主函数
def main(bpm, accent_every_n_beats):
    # 检查参数
    if bpm <= 0 or accent_every_n_beats <= 0:
        print("BPM and accent_every_n_beats must be greater than 0.")
        sys.exit(1)

    # 计算节拍间隔
    beat_interval = calculate_beat_interval(bpm)

    # 播放节拍
    for i in range(1, 1000):  # 限制循环次数以避免无限循环
        if i % accent_every_n_beats == 0:
            tone880.play(maxtime=100)  # 播放重音
        else:
            tone440.play(maxtime=100)  # 播放普通节拍

        pygame.time.wait(int(beat_interval))  # 等待节拍时间

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python metronome.py <bpm> <accent_every_n_beats>")
        sys.exit(1)

    bpm = int(sys.argv[1])
    accent_every_n_beats = int(sys.argv[2])

    main(bpm, accent_every_n_beats)